<?php

include_once('sidnepp/sidnEppAPI.php');
$eppClient = new SidnEppApi();

function sidnLogin() {
	global $eppClient;

	$avail = $eppClient->isAvailable();
	//try { $avail = $eppClient->isAvailable(); }
	//catch (Exception $e) { echo 'Caught exception: ',  $e->getMessage(), "\n"; }

	if($avail->getStatus() != 'success') {
	    echo "Status SIDN: Niet beschikbaar...\n";
	    exit;
	}
}

function sidnCreateContact($legalForm, $name, $street, $zipcode, $city, $countrycode, $email, $phoneNr, $compagnyName=NULL, $password=NULL) {
	global $eppClient;
	sidnLogin();

	$postalInfo = new SidnContactPostalInfo($name, $city, $countrycode, $compagnyName, $street, null, $zipcode);
	$contactInfo = new SidnContactInfo($postalInfo, $email, $legalForm, $password, $phoneNr);
	$result = $eppClient->createContact($contactInfo);
	$return = returnResult($result);
	if($return == 1) {
		return $result->getContact()->getContactHandle();
	} else {
		return $return;
	} 
}

function sidnRegDomain($domainName, $nameservers, $registrant, $admin, $tech, $customer=NULL) {
	global $eppClient;
	sidnLogin();

	$registrant = new SidnContact( $registrant );
	$admin = new SidnContact( $admin );
	$tech = new SidnContact( $tech );
	$registrant->setContactType(SidnContact::CONTACT_TYPE_REGISTRANT);
	$admin->setContactType(SidnContact::CONTACT_TYPE_ADMIN);
	$tech->setContactType(SidnContact::CONTACT_TYPE_TECH);

	$domainInfo = new SidnDomainInfo();
	$domainInfo->addContact($registrant);
	$domainInfo->addContact($admin);
	$domainInfo->addContact($tech);

	foreach($nameservers as $ns) {
		$domainInfo->addNameserver(new SidnNameserverInfo($ns));
	}
	$domain = new SidnDomain($domainName);
	$domain->setDomainInfo($domainInfo);
	$result = $eppClient->createDomain($domain);
	return returnResult($result);
}

function sidnTransferDomain($domain, $authcode) {
	global $eppClient;
	sidnLogin();

        $sidnDomain = new SidnDomain( $domain );
	$domainInfo = new SidnDomainInfo();
	$domainInfo->setAuthorisationCode($authcode);
	$sidnDomain->setDomainInfo($domainInfo);

	$result = $eppClient->requestDomainTransfer( $sidnDomain, $domain, $authcode );
	return returnResult($result);
}

function sidnDeleteNameservers($domain) {
	global $eppClient;
	sidnLogin();

	$domainInfoResult = $eppClient->getDomainInfo( new SidnDomain( $domain ) );
	if ( $domainInfoResult->getCode() == 1000 ) {
		$sidnDomain = $domainInfoResult->getDomain();
		$domainInfo = $sidnDomain->getDomainInfo();

		//first remove unwanted nameservers....
		$tempDomainInfo = new SidnDomainInfo();
		for ( $i = 0; $i <  $domainInfo->getNameserverLength(); $i++) {
			$ns = $domainInfo->getNameserver( $i )->getHostname();
			$tempDomainInfo->addNameserver( new SidnNameserverInfo( $ns ) );
		}

		$tempDomain = new SidnDomain( $domain );
		$tempDomain->setDomainInfo( $tempDomainInfo );
		$result = $eppClient->updateDomainRemoveNameserver( $tempDomain );
		return returnResult($result);
	} else {
		echo $domainInfoResult->getCode() . ': ' . $domainInfoResult->getStatusMessage();
	}
}

function sidnAddNameservers($domain, $nameservers) {
	global $eppClient;
	sidnLogin();

	$domainInfoResult = $eppClient->getDomainInfo( new SidnDomain( $domain ) );
	if ( $domainInfoResult->getCode() == 1000 ) {
		//add wanted nameservers....
		$tempDomainInfo = new SidnDomainInfo();
		foreach ($nameservers as $nameserver ) {
			if (!$nameserver) continue;
			$tempDomainInfo->addNameserver( new SidnNameserverInfo( $nameserver ) );
		}

		$tempDomain = new SidnDomain( $domain );
		$tempDomain->setDomainInfo( $tempDomainInfo );
		$result = $eppClient->updateDomainAddNameserver( $tempDomain );
		return returnResult($result);
	} else {
		return $domainInfoResult->getCode() . ': ' . $domainInfoResult->getStatusMessage();
	}
}

function sidnGetAuthCode($domain) {
	global $eppClient;
	sidnLogin();

	$domainInfoResult = $eppClient->getDomainInfo( new SidnDomain( $domain ) );
	if($domainInfoResult->getStatus() != 'error') {
		$sidnDomain = $domainInfoResult->getDomain();
		$domainInfo = $sidnDomain->getDomainInfo(); 
		$domainGetAuthCode = $domainInfo->getAuthorisationCode();

		return $domainGetAuthCode;
	} else {
		return $result->getCode() . ': ' . $domainInfoResult->getStatusMessage();
	}
}

function sidnQueryTransfer($domain) {
	global $eppClient;
	sidnLogin();
	
	$sidnDomain = new SidnDomain($domain);
	$result = $eppClient->queryDomainTransfer($sidnDomain, $domain);		
	return returnResult($result);
}

function sidnDeleteDomain($domain) {
	global $eppClient;
	sidnLogin();
	
	$sidnDomain = new SidnDomain($domain);
	$result = $eppClient->deleteDomain($sidnDomain);
	return returnResult($result);
}

function sidnPollMsgQueue() {
	global $eppClient;
	sidnLogin();

	$result = $eppClient->poll();
	return returnResult($result);
}

function sidnConfirmMsg($msg) {
	global $eppClient;
	sidnLogin();

	$result = $eppClient->confirmResponse($message);
	return returnResult($result);
}

function returnResult($result) {
	if (!($result instanceof SidnEppResult) ||  $result->getStatus() != SidnEppResult::SUCCESS) {
		return $result->getCode() . ': ' . $result->getStatusMessage();
	} else {
		return 'SUCCESS';
	}
}


?>
